package com.service;

import com.dao.GoodsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试事务的超时回滚
 */
@Service
public class GoodsService05 {
    @Autowired
    private GoodsDao goodsDao;


    /**
     * 测试 READ_COMMITTED 隔离级别情况 一个事务能够读取到另一个事务已提交的数据
     * @param user_id
     * @param goods_id
     * @param num
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.READ_COMMITTED,timeout = 3)
    public void buyGoodsByTxOvertime(int user_id,int goods_id,int num){
        //查询到商品价格
        Float goods_price = goodsDao.queryPriceById(goods_id);
        //购买商品，减去余额
        goodsDao.updateBalance02(user_id,goods_price * num);
        System.out.println("===超时===");
        try{
            Thread.sleep(1000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("===超时结束===");
        //更新库存
        goodsDao.updateAmount02(goods_id,num);
    }
}
